// pages/course/index.js
import {navTo,navToTab,onInput,onClick,showText} from '../../utils/reuse'
import{getshopTicketsList,cancelTickets} from '../../api/index'
import {formatTime} from '../../utils/util'
import Dialog from '../../miniprogram_npm/@vant/weapp/dialog/dialog';
import {baseURL} from '../../utils/request'
import drawQrcode from '../../utils/weapp.qrcode.esm.js'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    list:[],
    active: 1,
    page:1,
    pagesize:10,
    page_show:true,
    page_text:'加载中...',
    subscribeShow:false,
  },
  getCourseListFn(){
    var obj = {
      uid:wx.getStorageSync('uid'),
      page:this.data.page,
      pagesize:this.data.pagesize, 
      status: this.data.active,
      shop_id:wx.getStorageSync('shop_id'),
    }
    getshopTicketsList(obj).then(res=>{
      if(res.msg.length <= 0){
        return this.setData({
          page_show:false,
          page_text:'数据加载完毕！'
        })
      }else if(res.msg.length < this.data.pagesize){
        this.setData({
          page_show:false,
          page_text:'数据加载完毕！'
        })
      }
      if(res.sta == 1){
        res.msg.forEach(item=>{
          var arr = []
          if(item.goods.images){
            var arr1 = []
            arr = item.goods.images.split(',')
            arr.forEach(item=>{
              arr1.push(baseURL + item)
            })
            item.goods.images = arr1
            console.log(item)
          }
        })

        this.setData({
          list:[...this.data.list,...res.msg],
          // list:res.list
        })
      }
    })
  },
  click(e){
    onClick(e,this)
  },
  change(e){
    onInput(e,this)
  },
  QRsubmit(e){
    var that = this
    var id = e.currentTarget.dataset.id
    if(id){
      this.setData({
        subscribeShow:true
      })
      setTimeout(() => {
        const query = wx.createSelectorQuery()
        query.select('#myQrcode')
        .fields({
            node: true,
            size: true
        })
        .exec((res) => {
            var canvas = res[0].node
  
            // 调用方法drawQrcode生成二维码
            drawQrcode({
                canvas: canvas,
                canvasId: 'myQrcode',
                width: 260,
                padding: 30,
                background: '#ffffff',
                foreground: '#000000',
                text: `{"uid":${wx.getStorageSync('uid')},"order_id":${id},"current_time":${new Date().getTime()}}`,
            })
            // 获取临时路径（得到之后，想干嘛就干嘛了）
            wx.canvasToTempFilePath({
                canvasId: 'myQrcode',
                canvas: canvas,
                x: 0,
                y: 0,
                width: 260,
                height: 260,
                destWidth: 260,
                destHeight: 260,
                success(res) {
                  that.setData({
                    subscribeShow:true
                  })
                },
                fail(res) {
                    console.error(res)
                }
            })
        })
      }, 1000);

    }
  },
  submit(e){
    var id = e.currentTarget.dataset.id
    wx.navigateTo({
      url: '/pages/course_detail/index?id=' + id,
    })
  },
  onChange(event) {
    this.setData({
      list:[],
      page:1,
      pagesize:this.data.pagesize,
      active:event.detail.name,
      page_show:true
    })
    this.getCourseListFn()
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
      this.getCourseListFn()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.setData({
      list:[],
      page:1,
      pagesize:this.data.pagesize,
      page_show:true
    })
    this.getCourseListFn()
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    this.setData({
      page:this.data.page + 1,
      pagesize:this.data.pagesize
    })
    if(this.data.page_show){
      this.getCourseListFn()
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})